package com.yqg.protection.controller;


import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.entity.HistoricalData;
import com.yqg.protection.service.IHistoricalDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * HistoricalDataController 前端控制器
 * @author zxf
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/historical-data")
@Api(tags = "历史数据管理")
public class HistoricalDataController {

    @Resource
    private IHistoricalDataService historicalDataService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @ApiOperation("新增或更新历史数据")
    public Result save(@RequestBody HistoricalData historicalData) {
        if (ObjectUtil.isEmpty(historicalData.getId())){
            historicalData.setUpTime(LocalDateTimeUtil.now());
        }
        return Result.success(historicalDataService.saveOrUpdate(historicalData));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @ApiOperation("根据ID删除历史数据")
    public boolean delete(@PathVariable Integer id) {
        boolean historicalData =historicalDataService.removeById(id);
        return historicalData;
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    @ApiOperation("查询所有历史数据")
    public Result findAll() {
        QueryWrapper<HistoricalData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<HistoricalData> list=  historicalDataService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询历史数据")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(historicalDataService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    @ApiOperation("分页查询历史数据")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<HistoricalData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(historicalDataService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }



}

